package com.itheima.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itheima.Util.JwtUtil;
import com.itheima.dao.EmpMapper;
import com.itheima.po.LoginParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.LoginService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;


@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    EmpMapper empMapper;

    @Override
    public Result login(LoginParam param) {
        Emp login = empMapper.login(param.getUsername());
        if (Objects.isNull(login) || !login.getPassword().equals(param.getPassword())) {
            return Result.fail("用户名或密码错误");
        }
        //若账号密码正确就下发jwt令牌
        String token = JwtUtil.generateToken(login.getUsername(), login.getPassword(), 30);
        JSONObject result = new JSONObject();
        result.put("id", login.getId());
        result.put("username", login.getUsername());
        result.put("name", login.getName());
        result.put("token", token);
        return Result.success(result);
    }
}
